﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace Library
{
    public partial class LogIn : Form
    {
        public string connectionParamaters { get; private set; }
        public SourceType DBtype { get; private set; }


        public LogIn()
        {
            InitializeComponent();
            ConnectionString_txbx.Text = "Data Source =;Initial Catalog=;uid=;pwd=";
        }

        private void XML_rdbtn_CheckedChanged(object sender, EventArgs e)
        {
            if (XML_rdbtn.Checked == true)
            {
                XML_gpbx.Enabled = true;
                SQL_gpbx.Enabled = false;
            }
            else
            {
                XML_gpbx.Enabled = false;
                SQL_gpbx.Enabled = true;
            }
        }

        private void Cancel_btn_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void ConnectionParameters_txbx_Leave(object sender, EventArgs e)
        {
            if (UseWinAuth_chbx.Checked)
            {
                ConnectionString_txbx.Text = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True",
                    DataSource_txbx.Text.Trim(),InitCatalog_txbx.Text.Trim());
                Login_txbx.Enabled = false;
                Password_txbx.Enabled = false;
            }
            else
            {
                Login_txbx.Enabled = true;
                Password_txbx.Enabled = true;
                ConnectionString_txbx.Text = string.Format("Data Source={0};Initial Catalog={1};uid={2};pwd={3}",
                    DataSource_txbx.Text.Trim(), InitCatalog_txbx.Text.Trim(),Login_txbx.Text.Trim(),Password_txbx.Text.Trim());
            }
        }

        private void ChooseXmlFile_btn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openXML_fldg = new OpenFileDialog();
            openXML_fldg.FileName = string.Empty;
            openXML_fldg.Filter = "File XML (*.xml)|*.xml|All files (*.*)|*.*";

            if (openXML_fldg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                XmlFile_txbx.Text = openXML_fldg.FileName;

            
        }

        private void Connect_btn_Click(object sender, EventArgs e)
        {
            if (XML_rdbtn.Checked)
            {
                this.connectionParamaters = this.XmlFile_txbx.Text.Trim();
                this.DBtype = SourceType.XML;
            }
            else
            {
                if (DataSource_txbx.Text.Trim().Length == 0 || InitCatalog_txbx.Text.Trim().Length == 0)
                {
                    MessageBox.Show("You don't set Location Server or Target DB.");
                    return;
                }
                if (!UseWinAuth_chbx.Checked)
                {
                    if (Login_txbx.Text.Trim().Length == 0 || Password_txbx.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("You don't set Login or Password.");
                        return;
                    }
                }
                connectionParamaters = ConnectionString_txbx.Text.Trim();

                this.DBtype = SourceType.SQL;
            }
            DialogResult = DialogResult.OK;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = Guid.NewGuid().ToString();
        }
    }
}
